//
//  PlistTool.m
//  OnionHome
//
//  Created by fengjie on 2019/10/11.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "PlistTool.h"

//图文
#define k_ReleseImageAndText  @"ReleseImageAndText"
//文章
#define k_ReleseArticle  @"ReleseArticle"
//视频
#define k_ReleseVideo  @"ReleseVideo"
//小视频
#define k_ReleseSmallVideo  @"ReleseSmallVideo"
//提问
#define k_ReleseQanswer  @"ReleseQanswer"
//美食
#define k_ReleseFood  @"ReleseFood"

@implementation PlistTool
/**往plist文件中存储数据 */
+(void)savePlistValue:(NSDictionary *)valueDic Type:(ReleseType)type{
    NSMutableArray * array = [self getPlistFileData:type];
    if (!array) {
        array = [NSMutableArray array];
    }
    [array insertObject:valueDic atIndex:0];
    [array writeToFile:[self getFilePath:type] atomically:YES];

}
+(void)savePlistValue:(NSMutableDictionary *)valueDic Type:(ReleseType)type Images:(NSMutableArray *)images successful:(void(^)(NSString * message))success_block{
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSMutableArray * array = [NSMutableArray array];//图片地址数组
        for (NSInteger i = 0 ; i < images.count ; i++) {
            PhotoModel * model = images[i];
            NSString * name = [ImageAndVideoSaveTool get_ONLYID];
            [array addObject:name];
            [ImageAndVideoSaveTool saveImageDocuments:model.highDefinitionImage imageName:name];
        }
        if (array.count > 0) {
            [valueDic setValue:array forKey:@"images"];
        }
        [PlistTool savePlistValue:valueDic Type:type];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (success_block){
                DLogFunc;
                success_block(@"");
            }
        });
    });

}
/**根据key 获取plist 文件中缓存的数据*/
+(NSMutableArray *)getPlistType:(ReleseType)type{
    NSMutableArray * array = [self getPlistFileData:type];
    
    return array;
}

#pragma mark ----内部方法

/**
 获取plist文件中数据
 */
+(NSMutableArray *)getPlistFileData:(ReleseType)type{
    
    NSString * plistPath = [self getFilePath:type];
//    DLog(@"%@",plistPath);
//    NSMutableDictionary *dataDic = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
    NSMutableArray * array = [NSMutableArray arrayWithContentsOfFile:plistPath];
//    DLog(@"-----%@",dataDic);
    return array;
}

/**
 获取plist文件路径
 
 @return 返回一个路径
 */
+(NSString *)getFilePath:(ReleseType)type{
    
    NSString * dataCache = @"";
    
    if (type == ReleseImageAndText) {
        dataCache = k_ReleseImageAndText;
    }
    if (type == ReleseArticle) {
        dataCache = k_ReleseArticle;
    }
    if (type == ReleseVideo) {
        dataCache = k_ReleseVideo;
    }
    if (type == ReleseSmallVideo) {
        dataCache = k_ReleseSmallVideo;
    }
    if (type == ReleseQanswer) {
        dataCache = k_ReleseQanswer;
    }
    if (type == ReleseFood) {
        dataCache = k_ReleseFood;
    }
    
    NSArray *sandBoxPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [sandBoxPath objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",dataCache]];
    DLog(@"%@",plistPath);
    return plistPath;
}
@end
